package com.redis.client.controller;

import com.redis.client.manage.Persistent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 * @author hetao
 */
public class MainController {

    @FXML private TabPane tabPane;

    @FXML private TreeView treeView;

    @FXML private Button connectButton;

    public void initialize() throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("/fxml/info.fxml"));

        Tab tab = new Tab("info", root);
        tab.setClosable(true);
        tabPane.getTabs().add(tab);

        TreeItem<String> rootItem = new TreeItem<> ("Inbox");
        rootItem.setExpanded(true);

        for (int i = 1; i < 6; i++) {
            TreeItem<String> item = new TreeItem<> ("Message" + i);
            TreeItem<String> threeItem = new TreeItem<String>("第三级");
            item.getChildren().add(threeItem);
            rootItem.getChildren().add(item);
        }

        treeView.setRoot(rootItem);
        treeView.setShowRoot(false);
        treeView.setOnContextMenuRequested(event -> {
            TreeItem<String> item = (TreeItem<String>)treeView.getSelectionModel().getSelectedItem();
            System.out.println(item.getValue());
            System.out.println(((TreeItem<String>) treeView.getSelectionModel().getSelectedItem()).getParent());
            System.out.println(((TreeItem<String>) treeView.getSelectionModel().getSelectedItem()).getParent().getParent());
            ContextMenu contextMenu = new ContextMenu();
            MenuItem frameBegin = new MenuItem("设为开始帧");
            MenuItem frameEnd = new MenuItem("设为结束帧");
            contextMenu.getItems().addAll(frameBegin, frameEnd);

            contextMenu.show(treeView, event.getScreenX(), event.getScreenY());
        });
    }

    @FXML
    public void connectButtonAction() throws Exception {
//        Parent root = FXMLLoader.load(getClass().getResource("/fxml/server.fxml"));
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/server.fxml"));
        Parent root = loader.load();
        ServerController controller = loader.getController();
        controller.init(Persistent.getInstance().getAccounts().get(0));

        Stage stage = new Stage();
        stage.setScene(new Scene(root));
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setResizable(false);
        stage.show();

    }




}
